﻿using System;
using System.Collections.Generic;
using System.Linq;
using TopWinCMS.BLL;
using TopWinCMS.BLL.PageFactory;
using TopWinCMS.Common;
using TopWinCMS.Model;
using TopWinCMS.WebFactory;

namespace TopWinCMS.WebUI.Views.TopWinCMS6.Comment
{
    /// <summary>
    /// List 的摘要说明。
    /// </summary>
    public partial class List : PublicPage
    {
        int ContentID;
        protected CoreInfo model;
        private static readonly IP2Location address = new IP2Location();
        private static readonly CoreManage ClassBll = new CoreManage();
        protected void Page_Load(object sender, System.EventArgs e)
        {
            ContentID = int.Parse(Request["ContentID"]);


            model = ClassBll.GetBaseInfo(ContentID);
            this.Title = model.ContentTitle;
            model.Link = StaticFilePath.ContentShowUrl(model);
            if (!IsPostBack)
            {

                bool flag = ClassBll.Exists(ContentID);
                if (!flag)
                {
                    ErrClass.IndexErrRedirect("数据不存在！");
                }

                BindList(1);
            }
        }

        #region 绑定数据
        private void BindList(int pageNo)
        {
            PagingParam pp = new PagingParam {PageSize = this.PageNavigation1.PageSize, PageNo = pageNo};

            RP_Comment.DataSource = new CommentManage().BindList(ContentID, true, pp);
            RP_Comment.DataBind();
            PageNavigation1.RecordCount = pp.RecordCount;

            RP_CommentHot.DataSource = new CommentManage().HotComments(ContentID);
            RP_CommentHot.DataBind();

            RP_Week.DataSource = new CoreManage().HotContents(12, model.ClassID, "Week");
            RP_Week.DataBind();

            RP_Month.DataSource = new CoreManage().HotContents(12, model.ClassID, "Month");
            RP_Month.DataBind();

        }
        #endregion

        #region 添加数据
        protected void BT_OK_Click(object sender, EventArgs e)
        {
            //CommentInfo model = new CommentInfo();
            //model.ContentID = ContentID;
            //model.UserName = Server.HtmlEncode(TB_Name.Text);
            //model.Score = byte.Parse(DDL_Score.SelectedValue); 
            //model.Content = Server.HtmlEncode(TB_Content.Text);
            //model.IP = HttpClient.GetIP();


            //Result resu = new CommentManage().Add(model, ModelID);
            //if (string.IsNullOrEmpty(resu.Err) == false)
            //{
            //    MsgBox.Message(resu.Err,this);
            //    return;
            //}

            //if (model.Pass == 0)
            //{
            //    MsgBox.Message("提交成功。\\n待审核以后才能显示出来。", this);
            //}
            //else
            //{ 
            //    BindList(1);
            //    MsgBox.Message("提交成功。", this);
            //}

        }
        #endregion

        protected void PageNavigation1_PageChanging(object src, TopWinCMS.WebControls.PageChangingEventArgs e)
        {
            BindList(e.NewPageIndex);
        }

        protected string Address(object ip)
        {
            address.IP = ip.ToString();
            return address.IPLocation();
        }

        protected string GetContent(object objComment)
        {
            CommentInfo cmt = (CommentInfo)objComment;
            string output = "";
            IList<CommentInfo> quoteList = new CommentManage().GetFatherAndMe(cmt.Path);
            //quoteList.Add(cmt);
            //quoteList.AddRange(cmt.Children);

            for (int i = 0; i < quoteList.Count - 1; i++)
            {
                var quote = quoteList[i];
                output = String.Format(
                       "<div>{0}<h5><em class=\"num\">#{6}</em><a class=\"user-name\" target=\"_blank\" href=\"#\">{1}</a> {8} <em class=\"net\">[{7}]</em></h5><br />{2}<div class=\"bar\"><a href=\"#\" onclick=\"SupportOrOppose({3},true,this);return false;\">支持({4})</a>&nbsp;&nbsp;&nbsp;<a href=\"#\" onclick=\"SupportOrOppose({3},false,this);return false;\">反对({5})</a>&nbsp;&nbsp;&nbsp;<a href=\"javascript:ShowReply({3})\">回复</a></div></div>",
                       output, UserName(quote.UserName), quote.Content, quote.ID, quote.Support, quote.Oppose, i + 1, Address(quote.IP), quote.CreateTime);
            }

            var lastComment = quoteList.LastOrDefault();
            if (lastComment == null)
            {
                return cmt.Content;
            }
            // 添加当前引用
            output = String.Format(
                   "<div class='comment'>{0}<p>{1}</p></div><div class=\"bar\"><a href=\"#\" onclick=\"SupportOrOppose({3},true,this);return false;\">支持({4})</a>&nbsp;&nbsp;&nbsp;<a href=\"#\" onclick=\"SupportOrOppose({3},false,this);return false;\">反对({5})</a>&nbsp;&nbsp;&nbsp;<a href=\"javascript:ShowReply({3})\">回复</a></div>",
                   output, lastComment.Content, string.Empty, lastComment.ID, lastComment.Support, lastComment.Oppose);

            return output;
        }

        protected string UserName(object userName)
        {
            if (Convert.ToString(userName).Length == 0)
            {
                return "游客";
            }
            else
            {
                return userName.ToString();
            }
        }
    }
}